##

rm(list = ls())

library(tidyverse)

## Load data sets

res <- read_rds("data/Results_MediaConsumption.rds") %>%
    mutate(period = 0)

res_ll <- read_rds("data/Results_MediaConsumption_Dynamic.rds")

##

res <- bind_rows(
    res_ll,
    res
    ) %>%
    mutate(fe = dplyr::recode(fe,
        `none` = "Base model",
        `year` = "Year FE",
        `year_covars` = "Covars + Year FE"
    )) %>%
    mutate(fe = factor(fe, levels = c(
        "Base model",
        "Year FE",
        "Covars + Year FE"
    ))) %>%
    mutate(
        conf.low90 = estimate - qnorm(0.95) * std.error,
        conf.high90 = estimate + qnorm(0.95) * std.error
    ) %>%
    mutate(period = ifelse(period > -1, period + 1, period)) %>%
    filter(!period > 2)

# Combine

res_full <- res

## Rename outcomes but only if desired

o_list <- c(
    "TZ_u_faz_nw_bin",
    "TZ_u_bild_nw_bin",
    "TZ_u_sz_nw_bin", "TZ_u_welt_nw_bin",
    "index_nw_ratio_bin_national", "index_nw"
)

## ##

o_labs_diff <- o_list %>%
    paste0(., "_mean_diff")
o_labs_proper <- o_list

## Make df

dict_df <- data.frame(
    outcome = c(
        o_labs_diff
    ),
    outcome_proper = o_labs_proper
)

## PD

pd <- position_dodge(0.4)

#### Lag Lead plot ####

res_plot2 <- res_full %>%
    filter(str_detect(term, "Decrease")) %>%
    left_join(dict_df) %>%
    dplyr::select(-outcome) %>%
    dplyr::rename(outcome = outcome_proper) %>%
    filter(!is.na(outcome)) %>%
    mutate(outcome = as.character(outcome))

## Rename

res_plot2 <- res_plot2 %>%
    mutate(outcome_proper = dplyr::recode(outcome,
        `index_nw` =
            "National newspaper\nconsumption (absolute)",
        `index_nw_ratio_bin_national` =
            "National newspaper\nconsumption (share)"
    ))


## Figure 5

p2 <- ggplot(
    res_plot2 %>%
        filter(fe == "Year FE") %>%
        filter(outcome %in% c(
            "index_nw",
            "index_nw_ratio_bin_national"
        )), aes(period, estimate, outcome_version)
) +
    geom_hline(yintercept = 0, linetype = "dotted") +
    geom_vline(xintercept = 0, linetype = "dotted") +
    geom_errorbar(aes(ymin = conf.low90, ymax = conf.high90),
        position = pd, width = 0, linewidth = 1
    ) +
    geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
        position = pd, width = 0, linewidth = 0.5
    ) +
    geom_point(
        position = pd,
        shape = 21,
        size = 3,
        fill = "white"
    ) +
    facet_wrap(~outcome_proper, scales = "free") +
    theme(legend.position = "bottom") +
    theme_bw() +
    xlab("") +
    ylab("Effect of newspaper exit") +
    theme(legend.position = "bottom") +
    scale_x_continuous(breaks = -2:3) +
    xlab("Elections before and after newspaper exit")
p2


## Figure 6 

p3 <- ggplot(
    res_plot2 %>%
        filter(fe == "Year FE") %>%
        filter(str_detect(outcome, "bild|faz|sz|welt")) %>%
        filter(period %in% c(1, 2)) %>%
        mutate(outcome_proper = case_when(
            str_detect(outcome, "bild") ~ "Bild",
            str_detect(outcome, "faz") ~ "FAZ",
            str_detect(outcome, "sz") ~ "SZ",
            str_detect(outcome, "welt") ~ "Welt"
        )),
    aes(outcome_proper, estimate, period)
) +
    geom_hline(yintercept = 0, linetype = "dotted") +
    geom_errorbar(
        aes(
            ymin = conf.low90, ymax = conf.high90,
            color = factor(period)
        ),
        position = pd, width = 0, linewidth = 1
    ) +
    geom_errorbar(
        aes(
            ymin = conf.low, ymax = conf.high,
            color = factor(period)
        ),
        position = pd, width = 0, linewidth = 0.5
    ) +
    geom_point(
        position = pd,
        shape = 21,
        size = 3,
        aes(
            fill = factor(period),
            color = factor(period)
        )
    ) +
    theme(legend.position = "bottom") +
    theme_hanno() +
    xlab("") +
    ylab("Effect of newspaper exit") +
    theme(legend.position = "bottom") +
    scale_color_manual(
        name = "", labels = c(
            "One election (1-4 years)\nafter exit",
            "Two elections (5-8 years)\nafter exit"
        ),
        values = c("grey60", "black")
    ) +
    scale_fill_manual(
        name = "", labels = c(
            "One election (1-4 years)\nafter exit",
            "Two elections (5-8 years)\nafter exit"
        ),
        values = c("grey60", "black")
    ) +
    theme(axis.title.x = element_blank())
p3